﻿using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using HKB.Common;
using HKB.Web;
using HKB.Web.Admin;
using HKB.Web.DataHelper;
using HKB.Data;

public partial class Admin_Admin_AddAnnounce : Admin, IRequiresSessionState
{
    private int AnnounceID = 0;
    protected DefaultProfile profile
    {
        get
        {
            return (DefaultProfile)this.Context.Profile;
        }
    }
    protected global_asax applicationInstance
    {
        get
        {
            return (global_asax)this.Context.ApplicationInstance;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        this.AnnounceID = Fetch.GetID("AnnounceID");
        this.txtBeginDate.Attributes.Add("onClick", string.Concat(new string[]
		{
			"show_cele_date(",
			this.txtBeginDate.UniqueID,
			",'','',",
			this.txtBeginDate.UniqueID,
			");"
		}));
        this.txtEndDate.Attributes.Add("onClick", string.Concat(new string[]
		{
			"show_cele_date(",
			this.txtEndDate.UniqueID,
			",'','',",
			this.txtEndDate.UniqueID,
			");"
		}));
        if (!base.admin.CheckAdminPower(2))
        {
            base.WriteLog("Modify", Language.GetAdminMsg("Log/Access", this.AnnounceID.ToString()), "Announce");
            base.WriteRightErrMsg();
        }
        if (!base.IsPostBack)
        {
            if (this.AnnounceID != 0)
            {
                this.bandannouce(this.AnnounceID);
                this.Button1.Visible = false;
            }
            else
            {
                this.Button2.Visible = false;
                this.txtzuozhe.Text = base.admin.AdminName;
            }
        }
    }
    protected void bandannouce(int AnnounceID)
    {
        AnnounceBll announceBll = new AnnounceBll(this.conn);
        DataTable announceByAnnounceID = announceBll.GetAnnounceByAnnounceID(AnnounceID);
        if (announceByAnnounceID.Rows.Count > 0)
        {
            this.txtBeginDate.Text = ((announceByAnnounceID.Rows[0]["AnnounceBegin"] == DBNull.Value) ? DateTime.Now : Convert.ToDateTime(announceByAnnounceID.Rows[0]["AnnounceBegin"])).ToString();
            this.txtcontent.Text = announceByAnnounceID.Rows[0]["AnnounceContent"].ToString();
            DateTime dateTime = (announceByAnnounceID.Rows[0]["AnnounceEnd"] == DBNull.Value) ? DateTime.Now : Convert.ToDateTime(announceByAnnounceID.Rows[0]["AnnounceEnd"]);
            this.txtEndDate.Text = string.Concat(new object[]
			{
				dateTime.Year,
				"-",
				dateTime.Month,
				"-",
				dateTime.Day
			});
            this.txtTitle.Text = announceByAnnounceID.Rows[0]["AnnounceTitle"].ToString();
            string text = announceByAnnounceID.Rows[0]["AnnounceOwner"].ToString();
            if (text.Trim() == "")
            {
                text = base.admin.AdminName;
            }
            this.txtzuozhe.Text = text;
            this.dropImportant.SelectedValue = announceByAnnounceID.Rows[0]["AnnounceType"].ToString();
            this.droptype.SelectedValue = announceByAnnounceID.Rows[0]["AnnounceImportant"].ToString();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.CheckRight("AddNew");
        AnnounceBll announceBll = new AnnounceBll(this.conn);
        if (announceBll.AddAnnounce(this.txtTitle.Text, int.Parse(this.droptype.SelectedValue), int.Parse(this.dropImportant.SelectedValue), this.txtcontent.Text, this.txtzuozhe.Text, DateTime.Parse(this.txtBeginDate.Text), DateTime.Parse(this.txtEndDate.Text)))
        {
            this.WriteSuccessMsg("", Language.GetAdminMsg("Common/AddSuccess"), "Admin_Announce.aspx");
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        this.CheckRight("Modify");
        this.AnnounceID = Fetch.GetID("AnnounceID");
        if (this.AnnounceID != 0)
        {
            AnnounceBll announceBll = new AnnounceBll();
            if (announceBll.UpdateAnnounce(this.AnnounceID, this.txtTitle.Text, int.Parse(this.droptype.SelectedValue), int.Parse(this.dropImportant.SelectedValue), this.txtcontent.Text, this.txtzuozhe.Text, DateTime.Parse(this.txtBeginDate.Text), DateTime.Parse(this.txtEndDate.Text)))
            {
                this.WriteSuccessMsg("", Language.GetAdminMsg("Common/EditSuccess"), "Admin_Announce.aspx");
            }
        }
        else
        {
            this.WriteErrMsg("", Language.GetAdminMsg("Common/ParamErr"), "");
        }
    }
    protected bool CheckRight(string str)
    {
        if (!base.admin.CheckAdminPower(2))
        {
            base.WriteLog(str, Language.GetAdminMsg("Log/Access", this.AnnounceID.ToString()), "Announce");
            base.WriteRightErrMsg();
        }
        try
        {
            DateTime.Parse(this.txtBeginDate.Text);
            DateTime.Parse(this.txtEndDate.Text);
        }
        catch
        {
            this.WriteErrMsg("", Language.Get("Common/DateTimeFormatErr"), "");
        }
        return true;
    }
}
